package com.haiwei.poi.word.bean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haiwei.poi.common.HaiweiStringUtil;
import com.haiwei.poi.common.nassert.HaiweiAssert;
import com.haiwei.poi.word.exception.HaiweiBeanException;
import com.haiwei.poi.word.exception.HaiweiBeanInvalidTypeException;
import com.haiwei.poi.word.exception.HaiweiPoiWordGeneralException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 对象 节点.
 * Map 数据结构
 *
 * @date: 2022-06-27
 * @version: 1.0
 * @author: karry@haiwei.com
 */
@Data
@AllArgsConstructor
public class HaiweiObject extends JSONObject {
    
    public static HaiweiObject parseObject(String text) {
        HaiweiAssert.assertTrue("text can not bean blank", HaiweiStringUtil.isNotBlank(text));
        try {
            return (HaiweiObject)JSONObject.parseObject(text);
        } catch (Exception e) {
            throw new HaiweiBeanException("param type must bean json object");
        }
    }
    
    /**
     * 获取字符串
     * @param name
     * @return
     */
    public String getString(String name){
        HaiweiAssert.assertNotBlank(name);
        Object obj =  super.get(name);
        if(obj == null){
            return null;
        }
        if(obj instanceof String ){
            return obj.toString();
        }
        throw new HaiweiBeanInvalidTypeException("name[" + name + "] inliad type: " + obj.getClass().toGenericString());
    }
    
    /**
     * 获取数组
     * @param name
     * @return
     */
    public HaiweiObjectArray getArray(String name){
        HaiweiAssert.assertNotBlank(name);
        Object obj = super.get(name);
        if(obj == null){
            return null;
        }
        if(obj instanceof JSONArray){
            return (HaiweiObjectArray)obj;
        }
        throw new HaiweiBeanInvalidTypeException("name[" + name + "] inliad type: " + obj.getClass().toGenericString());
    }
    

}
